package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	reader()
}

func openFile() {
	file, err := os.Open("D:\\go_project\\golang_study\\basic\\文件操作\\test.txt")
	if err != nil {
		fmt.Println("open file fail,err:", err)
		return
	}
	defer file.Close()

}
func reader() {
	file, err := os.Open("D:\\go_project\\golang_study\\basic\\文件操作\\test.txt")
	if err != nil {
		fmt.Println("open file fail,err:", err)
		return
	}
	defer file.Close()
	// 读取固定长度的字节数
	var content []byte
	tmp := make([]byte, 4)
	for {
		_, err := file.Read(tmp)
		if err == io.EOF {
			fmt.Println("文件读完了")
			return
		}
		if err != nil {
			fmt.Println("read file failed err:", err)
			return
		}
		//fmt.Printf("读取了%d字节数据\n", read)
		content = append(content, tmp...)
		fmt.Print(string(tmp))
	}
}
